<&contentdata 
section_name = 'settings'
page_name = 'log_consolidation'
author = 'mrmt32'
type = 'page'
>

<div class='settingsPage'>
	<h2>Router Log Consolidation</h2>
	<form id='consolidationForm'>
	
			<&javascript delayed='true'>
			SendCommandToServer({action:'get_consolidation_rules'}, function(jsonResponse)
			{
				if (jsonResponse.isError)
				{
					$("#consolidationForm").html(jsonResponse.ErrorString);
				}
				else
				{
					$("#consolidationForm").submit(function(e)
					{
						e.preventDefault();
						e.stopPropagation();
						
						// Fist find which items where removed:
						var removedItems = "";
						$("tr.disabled").each(function(index, domElement)
						{
							removedItems += (index == 0? "":";") + $(domElement).attr("consolid");
						});
						
						// Now find items that are not removed (so they can be updated or inserted
						var newItems = "";
						$("tr:not(.disabled):has(input)").each(function(index, domElement)
						{
							var id = $(domElement).attr("consolid");
							
							// Calculate time in seconds
							var timeBeforeFormated = $(this).find("input[name=timeBefore]").val();
							var timeBeforeMultiple = $(this).find("select[name=timeBeforeType] option:selected").val();
							var timeBefore = Math.round(timeBeforeFormated * (timeBeforeMultiple / 1000));
							
							var resolutionFormated = $(this).find("input[name=resolution]").val();
							var resolutionMultiple = $(this).find("select[name=resolutionType] option:selected").val();
							var resolution = Math.round(resolutionFormated * (resolutionMultiple / 1000));
							
							newItems += (index == 0? "":";") + timeBefore + ":" + resolution + (isset(id)? ":" + id : "");
						});
						
						$.post(InterfaceLocation + "?action=set_consolidation_rules", {update: newItems, 'delete': removedItems}, function(jsonResponse)
						{
							ParseContentData(window.ContentData.blocks.settings_change_status_block, {jsonResponse:jsonResponse, dontShowValues: true}, function (output)
							{
								$(".settingsPage").html(output);
							});
							
						}, "json");
						
						ParseContentData(window.ContentData.blocks.loading_text_block, {}, function (output)
						{
							$(".settingsPage").html(output);
						});
						return false;
					});
					
					ParseContentData(window.ContentData.blocks.consolidation_settings_form_block, {ReturnData: jsonResponse.ReturnData}, function(output)
					{
						$("#consolidationForm").html(output);
					})
				}
			});
			<&/javascript>
	
	</form>
</div>


<&/contentdata>

<&contentdata
block_name = 'consolidation_settings_form_block'
author = 'mrmt32'
type = 'block'
>
<fieldset class="wide">
	<h3>Router Consolidation Rules</h3>
	<p>These rules govern how Sky Router Tool will consolidate your logs over time (to save space).</p>
	<p class="warningText">WARNING: These rules are retroactive and will take effect immediatly; make sure they are correct before saving them! You may lose data otherwise.</p>
	<p>
		<table>
			<thead>
				<tr>
					<th>Consolidate Before</th><th>To a resolution of</th><th/>
				</tr>
			</thead>
			<tbody id='consolTableBody'>
				<&javascript delayed='true'>
					// Gets an optimum time value in either min/sec/days/years etc
					function getTimeValue (time)
					{
						if (time >= 31536000000)
						{
							// Time is greater than or equal to 1 year
							this.type = "Years";
							this.time = time / 31536000000;
						}
						else if (time >= 2419200000)
						{
							// Time is greater than or equal to 28 days (~1 month)
							this.type = "Months";
							this.time = time / 2419200000;
						}
						else if (time >= 604800000000)
						{
							// Time is greater than or equal to 1 week
							this.type = "Weeks";
							this.time = time / 604800000;
						}
						else if (time >= 86400000)
						{
							// Time is greater than or equal to 1 day
							this.type = "Days";
							this.time = time / 86400000;
						}
						else if (time >= 3600000)
						{
							// Time is greater than or equal to 1 hour
							this.type = "Hours";
							this.time = time / 3600000;
						}
						else if (time >= 60000)
						{
							// Time is greater than or equal to 1 minute
							this.type = "Minutes";
							this.time = time / 60000;
						}
						else
						{
							this.type = "Seconds";
							this.time = time / 1000;
						}
						return this;
					}
					
					function createTimeOptions(name, defaultType)
					{
						var result = "";
						
						var options = ['Seconds', 'Minutes', 'Hours', 'Days'  , 'Weeks'  , 'Months'  , 'Years'    ];
						var values  = [1000     , 60000    , 3600000, 86400000, 604800000, 2419200000, 31536000000];
						
						result += "<select name='" + name + "'>";
						
						for (i in options)
						{
							result += "<option value='" + values[i] + "'" + (options[i] == defaultType? " selected='selected'":"") + ">" + options[i] + "</option>";
						}
						
						result += "</select>";
						
						return result;
					}
					
					for ( i in parameters.ReturnData )
					{
						var consolidationPeriod = parameters.ReturnData[i];
						var timeBefore = new getTimeValue(consolidationPeriod.Period);
						var resolution = new getTimeValue(consolidationPeriod.Resolution);
						
						output = "";
						output += "<tr class='" + (i%2 == 0? "" : "odd") + "' consolid='" +  consolidationPeriod.Id + "'>";
						output += "	<td><input size='5' type='text' name='timeBefore' value='" + timeBefore.time + "'/> ";
						output += 	createTimeOptions("timeBeforeType", timeBefore.type) + " ago</td>";
						
						output += "	<td><input size='5' type='text' name='resolution' value='" + resolution.time + "'/> ";
						output += 	createTimeOptions("resolutionType", resolution.type) + " per measurement.</td>";
						output += "	<td><button type='button' class='removeButton'>Remove</button></td>";
						output += "</tr>";
						
						$("#consolTableBody").append(output);
						
						// Add remove button event handler
						$("tr[consolid='" + consolidationPeriod.Id + "'] button.removeButton").bind('click', {id: consolidationPeriod.Id}, function(e)
						{
							if ($(this).html() == "Remove")
							{
								$(this).html("Undo");
								$("tr[consolid='" + e.data.id + "']").toggleClass("disabled", true);
								$("tr[consolid='" + e.data.id + "'] input").attr("disabled", true);
								$("tr[consolid='" + e.data.id + "'] select").attr("disabled", true);
							}
							else
							{
								$(this).html("Remove");
								$("tr[consolid='" + e.data.id + "']").toggleClass("disabled", false);
								$("tr[consolid='" + e.data.id + "'] input").attr("disabled", false);
								$("tr[consolid='" + e.data.id + "'] select").attr("disabled", false);
							}
						});
					}
					window.log_consolidation_table_count = parameters.ReturnData.length;
					
					$("#consolTableBody").append("<tr class='separator'><td/><td/><td><button type='button' class='addButton'>Add</button></td></tr>");
					
					$(".addButton").click(function()
					{
						output = "";
						output += "<tr class='" + (window.log_consolidation_table_count%2 == 0? "" : "odd") + "' id='newitem_" + window.log_consolidation_table_count + "'>";
						output += "	<td><input size='5' type='text' name='timeBefore' /> ";
						output += 	createTimeOptions("timeBeforeType", "Seconds") + " ago</td>";
						
						output += "	<td><input size='5' type='text' name='resolution' /> ";
						output += 	createTimeOptions("resolutionType", "Seconds") + " per measurement.</td>";
						output += "	<td><button type='button' class='removeButton'>Remove</button></td>";
						output += "</tr>";
						
						$("#consolTableBody").append(output);
						
						$("#newitem_" + window.log_consolidation_table_count + " button.removeButton").bind('click', {item: "#newitem_" + window.log_consolidation_table_count}, function(e)
						{
								$(e.data.item).remove();
						});
						
						window.log_consolidation_table_count++;
					});
				<&/javascript>
			</tbody>
		</table>
	</p>
</fieldset>
<fieldset class="wide">
	<p class='submitButton'><input type='submit' value='Save' /></p>
</fieldset>
<&/contentdata>